<!--
 * @Author: your name
 * @Date: 2022-03-28 20:17:00
 * @LastEditTime: 2022-08-03 19:40:01
 * @LastEditors: xiaobaobeibai 604070135@qq.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /admin-web/src/views/user_management/components/modal.vue
-->
<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" class="demo-ruleForm">
      <el-form-item label="选择图片位置" prop="projectId">
        <el-select v-model="ruleForm.projectId" style="width:100%" placeholder="请输入栏目名称">
          <el-option v-for="(item,index) in list" :key="index" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="上传图片" prop="url">
        <el-upload
          class="avatar-uploader"
          :action="action"
          :show-file-list="false"
          name="multipartFile"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="url" :src="url" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <el-input v-show="false" v-model="ruleForm.url" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="ruleForm.sort" placeholder="请输入序号" />
      </el-form-item>
      <el-form-item label="是否启用" prop="isEnable">
        <el-switch v-model="ruleForm.isEnable" active-value="0" inactive-value="1" />
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="resetForm('ruleForm')">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { REQUEST_LIST } from '@/api/requestList'
import { HTTP_URI } from '@/api/baseUrl'
export default {
  data() {
    return {
      action: HTTP_URI.UPLOADIMG,
      dialogVisible: false,
      title: '',
      url: '',
      ruleForm: {
        projectId: '',
        sort: '',
        url: '',
        isEnable: '0'
      },
      list: [],
      rules: {
        projectId: [{ required: true, message: '请选择栏目', trigger: 'blur' }],
        url: [{ required: true, message: '请上传图片', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        isEnable: [{ required: false, message: '请输入排序', trigger: 'blur' }]
      }
    }
  },
  methods: {
    open(page, data) {
      this.dialogVisible = true
      if (page === 'add') {
        this.title = '添加Banner图'
        this.list = data
      }
    },
    handleClose() {
      this.dialogVisible = false
      this.$refs.ruleForm.resetFields
    },
    handleAvatarSuccess(res, file) {
      this.ruleForm.url = res.data.fileName
      this.url = res.data.previewUrl + '/' + res.data.fileName
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 10

      if (!isJPG) {
        this.$message.error('图片只能是JPG或者PNG 格式')
      }
      if (!isLt2M) {
        this.$message.error('图片大小不能超过 10MB!')
      }
      return isJPG && isLt2M
    },

    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          REQUEST_LIST.REQUST_FCN('ADDIMAGES', this.ruleForm, true).then(
            (res) => {
              if (res.status === 200) {
                this.$emit('ok')
                this.$message.success('添加成功！')
                this.handleClose()
              }
            }
          )
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.handleClose()
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
